<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3
 * Time: 15:53
 */
@header("content-Type: text/html; charset=utf-8");
ini_set('date.timezone','Asia/Shanghai');
include 'publics.php';
//查询table2里面的内容

$sql = "select * from table2 where put_user_id = ".$_SESSION['t_id']." or get_user_id = ".$_SESSION['t_id']." and sn = 0;";
$mysql = new Mysql();
$date = $mysql -> select_all($sql);

//查询管理员内容
$sqlq = "select * from table2";
$datsl = $mysql ->select_all($sqlq);

//装入接收人和发送人
foreach ($date as $ig => $nb){
    $sql1 = "select username from table1 where t_id = ".$nb['put_user_id'];
    $dats = $mysql ->select_one($sql1);
    $date[$ig]['name'] = $dats['username'];

    $sql2 = "select username from table1 where t_id = ".$nb['get_user_id'];
    $datc = $mysql -> select_one($sql2);
    $date[$ig]['name1'] = $datc['username'];

}

//查询出联系人
$sql3 = "select * from table1 where sn = 1 or sn = 3";
$daa = $mysql ->select_all($sql3);

//装入管理员数据
foreach ($datsl as $kt => $hx){
    $sql10 = "select username from table1 where t_id = ".$hx['put_user_id'];
    $datsc = $mysql ->select_one($sql10);
    $datsl[$kt]['name'] = $datsc['username'];

    $sql20 = "select username from table1 where t_id = ".$hx['get_user_id'];
    $datcc = $mysql ->select_one($sql20);
    $datsl[$kt]['name1'] = $datcc['username'];

}
//新增
if (!empty($_POST)){
    $p_id = $_SESSION['t_id'];
    $g_id = $_POST['get_name_id'];
    $time = time();
    $text = ($_POST['text']);
    $sql4 = "insert into table2 (put_user_id,get_user_id,text,crete_time) values ($p_id,$g_id,'".$text."',$time);";
    $datt = array(
        'put_user_id' => $_SESSION['t_id'],
        'get_user_id' => $_POST['get_name_id'],
        'text' => ($_POST['text']),
        'crete_time' => time(),
    );
    $dss = $mysql -> insert($datt,'table2');

    if ($dss == true){
        jumps("发送成功",1,"message.php");
    }else{
        jumps("发送失败",1,"message.php");
    }
}

//删除
if(isset($_GET['p_id'])){
    $p_ids = $_GET['p_id'];
    //"delete from table2 where p_id = $p_ids";
    $where = "p_id = $p_ids";
    $datas = $mysql ->delete('table2',$where);
    if($datas == true){
        jumps("删除成功",0,"message.php");
    }else{
        jumps("删除失败",0,"message.php");
    }
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>留言</title>
    <script type="text/javascript" charset="utf-8" src="utf8-php/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="utf8-php/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="utf8-php/lang/zh-cn/zh-cn.js"></script>
</head>
<style>
    .dj{
        float: left;
        border: 2px solid red;
        padding: 30px;
        margin: 10px 20px;
        overflow: auto;
    }
    .dj span{
        display: block;
    }
    .dj a{
        float: right;
    }
    .lj{
        float: left;
        padding-top: 40px;
    }
    .emm{
        float: right;
        position: relative;
    }
    .ems{
        width: 645px;
        height: 120px;
    }
    .ing{
        position: absolute;
        right: 0;
        top: 494px;
    }
    #editor{
        height: 320px;
    }
    .posi{
        position: absolute;
        right: 44px;
        top: 494px;
    }
    .csj{
        float: left;
        border: 2px solid red;
        padding: 30px;
        margin: 10px 30px;
        overflow: auto;
    }
    .csj span{
        display: block;
    }
    .csj a{
        float: right;
    }

</style>
<script src="js/jquery.min.js"></script>
<body>
    <?php if ($_SESSION['sn'] == 3){  ?>
        <h1 style="color:darkviolet;text-align: center;">
           尊敬的管理员 <?php echo $_SESSION['username']; ?>,欢迎光临
            <a href="admini.php" style="float: right;color: deeppink;font-size: 10px;">点击进行管理</a>
        </h1>

        <div style="position: relative">
            <span style="position: absolute;color: blue;font-size: 24px;left:119px">发送记录</span>
        </div>
        <div style="width: 359px;display: inline-block" class="lj">
            <?php foreach ($date as $k => $v){ ?>
                <?php if ($_SESSION['username'] == $v['name']){ ?>
                    <div class="dj" style="box-shadow: 10px 10px 10px #ccc;height: 120px;">
                        <span>发送人：<?php echo $v['name'] ?></span>
                        <span>接收人：<?php echo $v['name1'] ?></span>
                        <span>时间：<?php echo date("Y-m-d H:i:s",$v['crete_time']); ?></span>
                        <span>内容:  <br> <?php echo $v['text']?></span>
                        <a href="message.php?p_id=<?php echo $v['p_id'] ?>">删除</a>
                    </div>
                    <a href="update.php?p_id=<?php echo $v['p_id'] ?>" class="lj">修改</a>
                <?php } ?>
            <?php } ?>
        </div>
        <div style="position: relative">
            <span style="position: absolute;color: blue;font-size: 24px;left:800px">接收记录</span>
        </div>
        <div style="width: 359px;display: inline-block;padding-left:310px " class="lj">
            <?php foreach ($date as $k => $v){ ?>
                <?php if ($_SESSION['username'] != $v['name']){ ?>
                    <div class="csj" style="box-shadow: 10px 10px 10px #ccc;height: 120px;">
                        <span>发送人：<?php echo $v['name'] ?></span>
                        <span>接收人：<?php echo $v['name1'] ?></span>
                        <span>时间：<?php echo date("Y-m-d H:i:s",$v['crete_time']); ?></span>
                        <span>内容:  <br> <?php echo $v['text']?></span>
                        <a href="message.php?p_id=<?php echo $v['p_id'] ?>">删除</a>
                    </div>
                <?php } ?>
            <?php } ?>
        </div>
    <?php }else{  ?>
        <h1 style="color: red;text-align: center;">
            <?php echo $_SESSION['username']; ?>,欢迎光临
        </h1>
        <div style="position: relative">
            <span style="position: absolute;color: blue;font-size: 24px;left:119px">发送记录</span>
        </div>
        <div style="width: 359px;display: inline-block" class="lj">
            <?php foreach ($date as $k => $v){ ?>
                <?php if ($_SESSION['username'] == $v['name']){ ?>
                    <div class="dj" style="box-shadow: 10px 10px 10px #ccc;height: 120px;">
                        <span>发送人：<?php echo $v['name'] ?></span>
                        <span>接收人：<?php echo $v['name1'] ?></span>
                        <span>时间：<?php echo date("Y-m-d H:i:s",$v['crete_time']); ?></span>
                        <span>内容:  <br> <?php echo $v['text']?></span>
                        <a href="message.php?p_id=<?php echo $v['p_id'] ?>">删除</a>
                    </div>
                    <a href="update.php?p_id=<?php echo $v['p_id'] ?>" class="lj">修改</a>
                <?php } ?>
            <?php } ?>
        </div>
        <div style="position: relative">
            <span style="position: absolute;color: blue;font-size: 24px;left:800px">接收记录</span>
        </div>
        <div style="width: 359px;display: inline-block;padding-left:310px " class="lj">
            <?php foreach ($date as $k => $v){ ?>
                <?php if ($_SESSION['username'] != $v['name']){ ?>
                    <div class="csj" style="box-shadow: 10px 10px 10px #ccc;height: 120px;">
                        <span>发送人：<?php echo $v['name'] ?></span>
                        <span>接收人：<?php echo $v['name1'] ?></span>
                        <span>时间：<?php echo date("Y-m-d H:i:s",$v['crete_time']); ?></span>
                        <span>内容:  <br> <?php echo $v['text']?></span>
                        <a href="message.php?p_id=<?php echo $v['p_id'] ?>">删除</a>
                    </div>
                <?php } ?>
            <?php } ?>
        </div>
    <?php }  ?>
    <form method="post" action="message.php" class="emm">
        <div class="ems">
            <script id="editor" type="text/plain" name="text"></script>
        </div>
        <select name="get_name_id" id="" class="posi">
            <?php
            foreach ($daa as $rng => $lj){?>
                <?php if($lj['t_id'] != $_SESSION['t_id']){ ?>
                    <option value="<?php echo $lj['t_id'] ?>">
                        <?php echo $lj['username']  ?>
                    </option>
                <?php } ?>
            <?php } ?>
        </select>
        <input type="submit" value="发送" onclick="return fsh()" class="ing">
    </form>

</body>
<script type="text/javascript">
    var ue = UE.getEditor('editor');
    function fsh() {
        var text =  UE.getEditor('editor').getContent();
        if (text == ''){
            alert('不能发送为空');
            return false;
        }
    }
</script>
</html>
